/*
 * Created by Christopher King
 * Copyright (C) 2007 Christopher King, All Rights Reserved.
 */

package us.cirion.adventure.engine;

/**
 * Direction represents movement. A player moves through the world by moving
 * through a series of rooms, each of which are linked to others in a certain
 * Direction. Directions include the four cardinal points (generally North,
 * South, East and West), as well as other directions that might be added for a
 * particular game, such as Up, Down, Port, Starboard, Hubwards, and
 * Widdershins.
 * 
 * @author Chris
 */
public class Direction extends Verb
{

	/**
	 * Indicates no cardinal direction.
	 */
	public static final int NONE = 0;
	/**
	 * Indicates the cardinal pointing up, usually corresponding to North.
	 */
	public static final int UP = 1;
	/**
	 * Indicates the cardinal pointing down, usually corresponding to South.
	 */
	public static final int DOWN = 2;
	/**
	 * Indicates the cardinal pointing left, usually corresponding to West.
	 */
	public static final int LEFT = 3;
	/**
	 * Indicates the cardinal pointing right, usually corresponding to East.
	 */
	public static final int RIGHT = 4;

	private int cardinal = NONE;

	/**
	 * Link this Direction with one of the four cardinals.
	 * 
	 * @param cardinal
	 */
	public void setCardinal(int cardinal)
	{
		this.cardinal = cardinal;
	}

	/**
	 * Indicates which of the four Cardinals corresponds with this Direction.
	 * 
	 * @return One of the cardinal points, or <code>NONE</code> if this is a
	 *         non-cardinal direction.
	 */
	public int getCardinal()
	{
		return cardinal;
	}

	/**
	 * Indicates whether the Direction command can take a target.
	 * 
	 * @return {@link Verb.NOUN_DENIED}, because Directions never take targets.
	 */
	public int takesTarget()
	{
		return Verb.NOUN_DENIED;
	}

	/**
	 * Utility function to convert between the numeric and string
	 * representations of the cardinal values. The returned string can be passed
	 * into {@link us.cirion.adventure.engine.Direction#getCardinalInt} and will return the original value.
	 * 
	 * @param cardinal The cardinal to be converted.
	 * @return A string corresponding to the provided cardinal. This is not
	 *         intended to be human-readable, but can be used in
	 *         program-readable resources.
	 */
	public static String getCardinalString(int cardinal)
	{
		String directionWord = null;
		if (cardinal == UP)
		{
			directionWord = "up";
		} else if (cardinal == DOWN)
		{
			directionWord = "down";
		} else if (cardinal == LEFT)
		{
			directionWord = "left";
		} else if (cardinal == RIGHT)
		{
			directionWord = "right";
		} else if (cardinal == NONE)
		{
			directionWord = "none";
		}
		return directionWord;
	}

	/**
	 * Utility function to convert between the string and numeric
	 * representations of the cardinal values. The returned integer can be
	 * passed into {@link #getCardinalString} and will return the original
	 * string.
	 * 
	 * @param cardinalName The cardinal to be converted.
	 * @return An integer corresponding to the provided cardinal.
	 */
	public static int getCardinalInt(String cardinalName)
	{
		int cardinal = NONE;
		if (cardinalName.equalsIgnoreCase("up"))
		{
			cardinal = UP;
		} else if (cardinalName.equalsIgnoreCase("down"))
		{
			cardinal = DOWN;
		} else if (cardinalName.equalsIgnoreCase("left"))
		{
			cardinal = LEFT;
		} else if (cardinalName.equalsIgnoreCase("right"))
		{
			cardinal = RIGHT;
		}
		return cardinal;
	}
}
